package net.tngou.iask.action.my;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.jdbc.OrderType;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Answer;
import net.tngou.iask.pojo.Filter;
import net.tngou.iask.pojo.Question;
import net.tngou.iask.pojo.Question_answer;
import net.tngou.iask.service.AnswerService;
import net.tngou.iask.service.QuestionService;

/**
 * 问题
 * @author tngou@tngou.net
 *
 */
public class QuestionAction extends BaseAction {

	/**
	 * 列表
	 */
	public void list() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		int type=(int) ask.getId();//类型
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		QuestionService service = new QuestionService();
		PageUtil page = service.getPage(ask.getPage(), ask.getSize() , user.getMain(), type, ask.getKeyword());
		
		root.put("page", page);
		root.put("type", type);
		printFreemarker("my/question_list.ftl", root);
	}
	
	/**
	 * 添加
	 */
	public void add() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		int type=(int) ask.getId();
		AnswerService service = new AnswerService();

		List<?> list0 = service.getList( Filter.class);
		root.put("list0", list0);
		root.put("type", type);
		printFreemarker("my/question_add.ftl", root);
				
	}
	
	/**
	 * 更新
	 */
	public void update() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}		
		if(ask.getId()==0){run_404();return;}
		Question question = new Question().get(ask.getId());
		if(question==null){run_404();return;}
		QuestionService  questionService = new QuestionService();
		AnswerService service = new AnswerService();
		List<?> filters =  questionService.getFilter(question.getId());
		root.put("filters", filters);
		List<?> list0 = service.getList( Filter.class);
		root.put("list0", list0);
		root.put("question", question);
		printFreemarker("my/question_update.ftl", root);
	}
	
	/**
	 * 显示
	 */
	public void show() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		if(ask.getId()==0){run_404();return;}
		Question question = new Question().get(ask.getId());
		if(question==null){run_404();return;}
		QuestionService  questionService = new QuestionService();
		List<Answer> list =null;
		switch (question.getType()) {
		case 1:
			list=questionService.getAnswer(question.getId());
			break;
		case 2:
			list=questionService.getAnswer(question.getId());
			break;
		case 3:
			list=questionService.getAnswer(question.getId());
			break;
		default:
			break;
		}
		
	
		root.put("list", list);
		root.put("question", question);
		printFreemarker("my/question_show.ftl", root);
	}
	
	/**
	 * 修改选项顺序
	 */
	public void seq() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		if(ask.getId()==0){run_404();return;}
		Question question = new Question().get(ask.getId());
		if(question==null){run_404();return;}
		QuestionService  service = new QuestionService();


		List<Map<String, Object>> list =null;
		switch (question.getType()) {
		case 1:
			list=service.getQuestionAnswer(question.getId());
			break;
		case 2:
			list=service.getQuestionAnswer(question.getId());
			break;
		case 3:
			list=service.getQuestionAnswer(question.getId());
			break;
		default:
			break;
		}
		
		root.put("list", list);
		root.put("question", question);
		printFreemarker("my/question_seq.ftl", root);
	}
	
	@Deprecated
	public void sequpdate() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		if(ask.getId()==0){run_404();return;}
		Question_answer question_answer = new Question_answer().get(ask.getId());
		if(question_answer==null){run_404();return;}
		Question question=new Question().get(question_answer.getQuestion());
		Answer answer=new Answer().get(question_answer.getAnswer());
		if(question==null||answer==null){run_404();return;}
		root.put("question", question);
		root.put("answer", answer);
		root.put("question_answer", question_answer);
		printFreemarker("my/question_sequpdate.ftl", root);
	}
	
	/**
	 * 添加问题选项
	 */
	public void answeradd() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		if(ask.getId()==0){run_404();return;}
		Question question = new Question().get(ask.getId());
		if(question==null){run_404();return;}
		QuestionService  questionService = new QuestionService();
		PageUtil page =null;
		switch (question.getType()) {
		case 1:
		
			page=questionService.getPage(ask.getPage(), ask.getSize(), user.getMain(), question.getId(), 0, ask.getKeyword());
			break;
		case 2:
			page=questionService.getPage(ask.getPage(), ask.getSize(), user.getMain(), question.getId(), 0, ask.getKeyword());
			break;
		case 3:
			page=questionService.getPage(ask.getPage(), ask.getSize(), user.getMain(), question.getId(), 1, ask.getKeyword());
			break;
		default:
			break;
		}

		root.put("page", page);
		root.put("question", question);
		printFreemarker("my/question_answeradd.ftl", root);
	}
}
